package com.ggh.jinjilive.ui.mall.goods.details;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.mobile.mall.MallPointsClass;
import com.ggh.jinjilive.mobile.mall.goods.GoodsDetailsMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 商品详情
 */
public class GoodsDetailsViewModel extends BaseViewModel{
    //商品详情
    public MutableLiveData<GoodsDetailsMobile> mGoodsDetailsMobile = new MutableLiveData<>();
    //收藏
    public MutableLiveData<Boolean> mCollection = new MutableLiveData<>();
    //规格
    public ObservableField<GoodsSpecificationMobile> mGoodsSpecificationMobile = new ObservableField<>();

    public GoodsDetailsViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }


    /**
     * 商品详情
     * @param id
     */
    public void productInfo(int id){
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        addSubscribe(apiCallInterface.productInfo(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<GoodsDetailsMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                GoodsDetailsMobile goodsDetailsMobile = (GoodsDetailsMobile) resultEntity.getData();
                mGoodsDetailsMobile.setValue(goodsDetailsMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mGoodsDetailsMobile.setValue(null);
            }
        }));
    }


    /**
     * 取消收藏成功
     * @param ids
     */
    public void delCollectProduct(int ids){
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        addSubscribe(apiCallInterface.delCollectProduct(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                mCollection.setValue(false);
                ToastUtils.make().show("已取消收藏");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }

    /**
     * 收藏成功
     * @param pid
     */
    public void addCollectProduct(int pid){
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        addSubscribe(apiCallInterface.addCollectProduct(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                mCollection.setValue(true);
                ToastUtils.make().show("收藏成功");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }

    /**
     * 加入购物车
     */
    public void addCart(Map<String, Object> map){
        addSubscribe(apiCallInterface.addCart(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show("已添加到购物车");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }



}
